package com.cswnode.demo.service.impl;

import com.cswnode.demo.dao.ProfessionDao;
import com.cswnode.demo.entity.Profession;
import com.cswnode.demo.enums.YesOrNoEnum;
import com.cswnode.demo.service.ProfessionService;
import com.cswnode.demo.util.Utils;
import com.cswnode.demo.vo.ProfessionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.Date;
import java.util.List;

/**
 * 专业(Profession)表服务实现类
 *
 * @author makejava
 * @since 2021-01-26 11:10:20
 */
@Service
public class ProfessionServiceImpl implements ProfessionService {
    @Autowired
    private ProfessionDao professionDao;
    /**
     * 事物
     */
    @Autowired
    private DataSourceTransactionManager transactionManager;

    @Override
    public List<Profession> query(String proName) {
        return professionDao.query(proName);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param proId 主键
     * @return 实例对象
     */
    @Override
    public Profession queryById(String proId) {
        return this.professionDao.queryById(proId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Profession> queryAllByLimit(int offset, int limit) {
        return this.professionDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<ProfessionVo> queryVoByLimit(int offset, int limit, String proName) {
        List<ProfessionVo> professionVos = professionDao.queryVoByLimit(offset, limit, proName);
        return professionVos;
    }

    @Override
    public int queryNum(String proName) {
        int num = professionDao.queryNum(proName);
        return num;
    }

    /**
     * 新增数据
     *
     * @param profession 实例对象
     * @return 实例对象
     */
    @Override
    public Profession insert(Profession profession) {
        if (StringUtils.isEmpty(profession.getProName())){
            return null;
        }
        String proId = Utils.GetUUID();
        profession.setProId(proId);
        profession.setCreateTime(new Date());
        profession.setIsDelete(YesOrNoEnum.NO.getCode());
        this.professionDao.insert(profession);
        return profession;
    }

    /**
     * 修改数据
     *
     * @param profession 实例对象
     * @return 实例对象
     */
    @Override
    public String update(Profession profession) {
        profession.setCreateTime(null);
        int update = professionDao.update(profession);
        if (update == 0){
            return "修改失败";
        }
        return null;
    }

    /**
     * 通过主键删除数据
     *
     * @param proId 主键
     * @return 是否成功
     */

    @Override
    @Transactional
    public boolean deleteById(String proId) {
        if (StringUtils.isEmpty(proId)){
            return false;
        }
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setName("SomeTxName");
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus status = transactionManager.getTransaction(def);
        try{
            professionDao.deleteById(proId);
            professionDao.deleteStu(proId);
            professionDao.deleteClass(proId);
        }catch (Exception e){
            transactionManager.rollback(status);
            throw e;
        }
        return true;
    }
}